import 'package:flutter/material.dart';
import 'package:wayofdressing/all_information/user_information.dart';
import 'package:wayofdressing/screens/ai_dress_screen.dart';
import 'package:wayofdressing/screens/aichat_screen.dart';
import 'package:wayofdressing/screens/dress_up_screen.dart';
import 'package:wayofdressing/screens/match_screen.dart';
import 'package:wayofdressing/screens/closet_screen.dart';
import 'package:wayofdressing/screens/community_screen.dart';
import 'package:wayofdressing/screens/own_screen.dart';
import 'package:wayofdressing/screens/raise_screen.dart';
import 'package:wayofdressing/screens/store_screen.dart';
import 'package:wayofdressing/main_navigator_widget.dart';
import 'package:wayofdressing/screens/calender_screen.dart';
import 'package:wayofdressing/screens/my_showcase.dart';
import 'package:wayofdressing/screens/market_screen.dart';
import 'package:wayofdressing/screens/my_dress_screen.dart';
import 'package:wayofdressing/screens/chat_screen.dart';
import 'package:wayofdressing/screens/about_us_screen.dart';
import 'package:wayofdressing/screens/init_screen.dart';
import 'package:wayofdressing/screens/login_screen.dart';
import 'package:wayofdressing/screens/register_screen.dart';
import 'package:dio/dio.dart';
import 'package:wayofdressing/dio/configureDio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wayofdressing/dio/UserDio.dart';
import 'package:wayofdressing/screens/post_screen.dart';
import 'package:wayofdressing/screens/body_manage_screen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  SharedPreferences prefs = await SharedPreferences.getInstance();
  password = prefs.getString('password')??'';
  account= prefs.getString('account')??'';

  bool isLoggedIn = false;

  if (name != '' && password != '') {
    try {
      Response<dynamic> response = await getUserByAccount(account, dio);
      dynamic responseData = response.data;
      if (responseData != null && responseData['success'] == true) {
        dynamic userData = responseData['data'];
        if (userData != null && userData['password'] == password) {
          prefs.setInt('id', userData['id']);
          isLoggedIn = true;
        }
      }
    } catch (e) {
      print('Error during auto-login: $e');
    }
  }

  runApp(MyApp(initialRoute: isLoggedIn ? MainNavigatorWidget.id : InitScreen.id));
}

class MyApp extends StatefulWidget {
  final String initialRoute;

  MyApp({required this.initialRoute});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title:"peanut",
      initialRoute: widget.initialRoute,
      routes: {
        MainNavigatorWidget.id: (context) => MainNavigatorWidget(),
        AiChatScreen.id: (context) => AiChatScreen(),
        MatchScreen.id: (context) => MatchScreen(),
        ClosetScreen.id: (context) => ClosetScreen(),
        RaiseScreen.id: (context) => RaiseScreen(),
        CommunityScreen.id: (context) => CommunityScreen(),
        StoreScreen.id: (context) => StoreScreen(),
        CalenderScreen.id: (context) => CalenderScreen(),
        MyShowcase.id: (context) => MyShowcase(),
        MarketScreen.id: (context) => MarketScreen(),
        MyDressScreen.id: (context) => MyDressScreen(),
        ChatScreen.id: (context) => ChatScreen(),
        AboutUs.id: (context) => AboutUs(),
        InitScreen.id: (context) => InitScreen(),
        LoginScreen.id: (context) => LoginScreen(),
        RegisterScreen.id: (context) => RegisterScreen(),
        PostScreen.id:(context)=>PostScreen(),
        BodyManageScreen.id:(context)=>BodyManageScreen(),
        AiDressScreen.id:(context)=>AiDressScreen(),
        DressUpScreen.id:(context)=>DressUpScreen(),
        OwnScreen.id:(context)=>OwnScreen(),
      },
    );
  }
}
